﻿#include <iostream>
#include <cstring>
#pragma warning(disable : 4996)//暂时关闭4996错误

class Mystring {
private:
    char* data;
    int length;

public:
    // 默认构造函数
    Mystring() {
        data = new char[1];
        data[0] = '\0';
        length = 0;
    }

    // 带参数的构造函数，从用户输入构建字符串
    Mystring(const char* str) {
        if (str == nullptr) {
            length = 0;
            data = new char[1];
            data[0] = '\0';//构建空的字符数组
        }
        else {
            length = strlen(str);
            data = new char[length + 1];
            strcpy(data, str);//输入的字符串被copy到data所指的内存中
        }
    }

    // 析构函数，此函数用于释放data所指的动态分配的内存
    //当Mystring对象的生命周期结束时，这个函数会被自动调用，防止内存泄漏
    ~Mystring() {
        delete[] data;
    }

    // 获取字符串长度
    int size() const {
        return length;
    }

    // 字符串拼接
    Mystring operator+(const Mystring& other) const {
        Mystring result;
        //构造一个新的Mystring对象result
        result.length = length + other.length;
        result.data = new char[result.length + 1];
        //分配足够的data内存
        int i;
        for (i = 0; i < length; i++) {
            result.data[i] = data[i];
        }
        for (int j = 0; j < other.length; j++) {
            result.data[i + j] = other.data[j];
        }
        //将当前对象的字符串内容和传入对象的字符串内容一次复制到result的data中
        result.data[result.length] = '\0';
        return result;
    }

    // 字符串大小比较
    bool operator<(const Mystring& other) const {
        for (int i = 0; i < std::min(length, other.length); i++) {
            if (data[i] < other.data[i]) {
                return true;
            }
            else if (data[i] > other.data[i]) {
                return false;
            }
        }
        return length < other.length;
    }

    bool operator>(const Mystring& other) const {
        return other < *this;
    }//如果另一个对象小于当前对象，则当前对象大于另一个对象

    bool operator<=(const Mystring& other) const {
        return!(*this > other);
    }

    bool operator>=(const Mystring& other) const {
        return!(*this < other);
    }

    bool operator==(const Mystring& other) const {
        if (length != other.length) {
            return false;
        }
        for (int i = 0; i < length; i++) {
            if (data[i] != other.data[i]) {
                return false;
            }
        }
        return true;
    }

    // 查找字符
    int find(char c) const {
        for (int i = 0; i < length; i++) {
            if (data[i] == c) {
                return i;
            }
        }
        return -1;
    }

    // 替换字符
    void replace(char oldChar, char newChar) {
        for (int i = 0; i < length; i++) {
            if (data[i] == oldChar) {
                data[i] = newChar;
            }
        }
    }

    void print() const {
        std::cout << data << std::endl;
    }
};

int main() {
    char input1[100];
    char input2[100];
    std::cout << "请输入第一个字符串: ";
    std::cin.getline(input1, 100);
    std::cout << "请输入第二个字符串: ";
    std::cin.getline(input2, 100);
    Mystring s1(input1);
    Mystring s2(input2);
    Mystring s3 = s1 + s2;
    s3.print();
    std::cout << "Length of s3: " << s3.size() << std::endl;
    Mystring s4(input1);
    Mystring s5(input2);
    std::cout << "(s4 < s5) is " << (s4 < s5) << std::endl;
    std::cout << "Index of a character in s3: ";
    char ch;
    std::cin >> ch;
    std::cout << "Index of '" << ch << "' in s3: " << s3.find(ch) << std::endl;
    std::cout << "Replace a character in s3. Enter the character to replace: ";
    char oldCh;
    std::cin >> oldCh;
    std::cout << "Enter the new character: ";
    char newCh;
    std::cin >> newCh;
    s3.replace(oldCh, newCh);
    s3.print();
    return 0;
}